<?php
declare (strict_types = 1);

namespace App\Logic;

use App\Event\AdministratorLogsEvent;
use App\Models\Role as RoleModel;
use App\Models\Menu as MenuModel;
use Illuminate\Support\Facades\DB;

class Role
{
    public function count(array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where['role_name'])) $map[] = ['role_name', 'like', '%'.$where['role_name'].'%'];
        return RoleModel::where($map)->count();
    }

    public function list(int $start, int $limit, array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where['role_name'])) $map[] = ['role_name', 'like', '%'.$where['role_name'].'%'];
        return RoleModel::with('accounts')->where($map)->orderBy('id','desc')->offset($start)->limit($limit)->get();
    }

    public function add(array $data)
    {
        $model = new RoleModel();
        $model->role_name = $data['role_name'];
        $model->role_intro = $data['role_intro'];
        $model->role_sort = $data['role_sort'];
        DB::beginTransaction();
        try {
            $model->save();
            if (!empty($data['auth'])) $model->menus()->sync($data['auth']);
            DB::commit();
            event(new AdministratorLogsEvent("添加后台角色"));
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return false;
        }
    }

    public function update(int $id, array $data): bool
    {
        $model = RoleModel::find($id);
        $model->role_name = $data['role_name'];
        $model->role_intro = $data['role_intro'];
        $model->role_sort = $data['role_sort'];
        DB::beginTransaction();
        try {
            $model->save();
            if(!empty($model->menus())) $model->menus()->detach();
            if (!empty($data['auth'])) $model->menus()->sync($data['auth']);
            DB::commit();
            event(new AdministratorLogsEvent("修改角色信息"));
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return false;
        }
    }

    public function delete(int $id): bool
    {
        $model = RoleModel::find($id);
        DB::beginTransaction();
        try {
            if(!empty($model->menus())) $model->menus()->detach();
            $model->delete();
            DB::commit();
            event(new AdministratorLogsEvent("删除后台角色"));
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return false;
        }
    }

    public function checkName(string $name, int $id = 0)
    {
        $model = RoleModel::where([['role_name', '=', $name], ['id', '<>', $id]])->first();
        if (empty($model)) return true;
        return false;
    }

    public function all()
    {
        return RoleModel::select('id','role_name','role_intro')->orderBy('role_sort')->get();
    }

    public function has($id)
    {
        $model = RoleModel::find($id);
        if (empty($model)) return false;
        return true;
    }

    public function findToArray(int $id)
    {
        $role = RoleModel::with('accounts')->with('menus')->find($id);
        if (1 == $id) {
            $menus = MenuModel::get();
        } else {
            $menus = $role['menus'];
        }
        $menusIds = array();
        foreach ($menus as $menu) {
            $menusIds[] = $menu['id'];
        }
        $role['menus'] = $menusIds;
        return $role;
    }

    public function findMenuIdsByRoleId(int $roleId)
    {
        $role = RoleModel::find($roleId);
        return $role->menus()->pluck('id')->toArray();
    }


}
